package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.customview.ListViewForScrollView;
import com.cloudsigns.factorymanager2.produce.adapter.Produce_ProductInfoAdapter;
import com.cloudsigns.factorymanager2.produce.bean.ProduceProduceOrderBean;
import com.cloudsigns.factorymanager2.produce.iview.IProductOrderReceiveView;
import com.cloudsigns.factorymanager2.produce.presenter.ProductOrderReceivePresenter;
import com.cloudsigns.factorymanager2.project.iview.IGetProduceOrderDetailView;
import com.cloudsigns.factorymanager2.project.presenter.GetProduceOrderDetailPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

public class ProduceReceiveOrderItemDetailActivity extends BaseActivity implements IGetProduceOrderDetailView, IProductOrderReceiveView {
    private String TAG = ProduceReceiveOrderItemDetailActivity.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_project_name, tv_engineering_name;
    private ImageView img_arrow_down;
    private LinearLayout ll_content;
    private TextView tv_type, tv_level, tv_completed_date, tv_dispath_person, tv_dispatch_time;
    private LinearLayout ll_receive_people;
    private TextView tv_receive_people_colon, tv_receive_time_colon, tv_receive_people, tv_receive_time;
    private ListViewForScrollView lv;
    private LinearLayout ll_bottom;
    private TextView btn_receive_order, btn_back_order;

    private GetProduceOrderDetailPresenter getProduceOrderDetailPresenter;
    private Produce_ProductInfoAdapter adapter;
    private ProduceProduceOrderBean produceOrderBean;
    private ProductOrderReceivePresenter productOrderReceivePresenter;
    private boolean isClick = true;
    private CommonEnum.STATUS status;

    private String orderId;
    private String class_type;
    private String selectedProductId;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_receive_order_item_detail);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_project_name = findViewById(R.id.tv_project_name);
        tv_engineering_name = findViewById(R.id.tv_engineering_name);
        img_arrow_down = findViewById(R.id.img_arrow_down);
        ll_content = findViewById(R.id.ll_content);
        tv_type = findViewById(R.id.tv_type);
        tv_level = findViewById(R.id.tv_level);
        tv_completed_date = findViewById(R.id.tv_completed_date);
        tv_dispath_person = findViewById(R.id.tv_dispath_person);
        tv_dispatch_time = findViewById(R.id.tv_dispatch_time);
        ll_receive_people = findViewById(R.id.ll_receive_people);
        tv_receive_people_colon = findViewById(R.id.tv_receive_people_colon);
        tv_receive_time_colon = findViewById(R.id.tv_receive_time_colon);
        tv_receive_people = findViewById(R.id.tv_receive_people);
        tv_receive_time = findViewById(R.id.tv_receive_time);
        lv = findViewById(R.id.produce_receive_lv);
        ll_bottom = findViewById(R.id.ll_bottom);
        btn_receive_order = findViewById(R.id.btn_receive_order);
        btn_back_order = findViewById(R.id.btn_back_order);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

        img_arrow_down.setBackgroundResource(R.mipmap.arrow_down_icon);

        Intent intent = getIntent();
        if (null != intent) {
            produceOrderBean = (ProduceProduceOrderBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
            class_type = intent.getStringExtra(ConstantUtil.KEY_2);
            selectedProductId = intent.getStringExtra("productId");
        }

        if ("ProduceEditOrderActivity".equals(class_type)) {
            orderId= intent.getStringExtra("orderId");
        }else{
            orderId=produceOrderBean.getOrderId();
        }

        if ("ProduceReceiveOrderWaitingFragment".equals(class_type)) {
            tv_title.setText(getResources().getString(R.string.receive_order));
            ll_receive_people.setVisibility(View.GONE);
            ll_bottom.setVisibility(View.VISIBLE);
        } else if ("ProduceReceiveOrderAlreadyFragment".equals(class_type)) {
            tv_title.setText(getResources().getString(R.string.receive_order));
            ll_receive_people.setVisibility(View.VISIBLE);
            ll_bottom.setVisibility(View.GONE);
            tv_receive_people_colon.setText(getResources().getString(R.string.receive_people_colon));
            tv_receive_time_colon.setText(getResources().getString(R.string.reception_time_colon));
        } else if ("ProduceReceiveOrderBackFragment".equals(class_type)) {
            tv_title.setText(getResources().getString(R.string.receive_order));
            ll_receive_people.setVisibility(View.VISIBLE);
            ll_bottom.setVisibility(View.GONE);
            tv_receive_people_colon.setText(getResources().getString(R.string.back_people));
            tv_receive_time_colon.setText(getResources().getString(R.string.back_time));
        } else if ("ProduceEditOrderActivity".equals(class_type)) {
            tv_title.setText(getResources().getString(R.string.order_information));
            ll_receive_people.setVisibility(View.GONE);
            ll_bottom.setVisibility(View.GONE);
        }

        getProduceOrderDetailPresenter = new GetProduceOrderDetailPresenter(this);
        getProduceOrderDetailPresenter.getProduceOrderDetail();

        adapter = new Produce_ProductInfoAdapter(this, TAG);
        lv.setAdapter(adapter);

        productOrderReceivePresenter = new ProductOrderReceivePresenter(this);
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        img_arrow_down.setOnClickListener(this);
        btn_receive_order.setOnClickListener(this);
        btn_back_order.setOnClickListener(this);
    }

    private void showArrowDown() {
        if (ll_content.getVisibility() == View.VISIBLE) {
            ll_content.setVisibility(View.GONE);
            img_arrow_down.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_content.setVisibility(View.VISIBLE);
            img_arrow_down.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    private void receiveOrder() {
        status = CommonEnum.STATUS.RECEIVED;
        isClick = false;
        setButton();
        productOrderReceivePresenter.productOrderReceive();
    }

    private void backOrder() {
        status = CommonEnum.STATUS.RETURNED;
        isClick = false;
        setButton();
        productOrderReceivePresenter.productOrderReceive();
    }

    private void setButton() {
        if (isClick) {
            btn_receive_order.setClickable(false);
            btn_receive_order.setBackgroundResource(R.drawable.btn_blue_save);
            btn_back_order.setClickable(false);
            btn_back_order.setBackgroundResource(R.drawable.btn_red_delete);
        } else {
            btn_receive_order.setClickable(false);
            btn_receive_order.setBackgroundResource(R.color.rgb_ffffffff);
            btn_back_order.setClickable(false);
            btn_back_order.setBackgroundResource(R.color.rgb_ffffffff);
        }
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.img_arrow_down:
                showArrowDown();
                break;
            // 接收工单
            case R.id.btn_receive_order:
                if (isClick)
                    receiveOrder();
                break;
            // 退回工单
            case R.id.btn_back_order:
                if (isClick)
                    backOrder();
                break;
        }
    }

    @Override
    public String getOrderId() {
        return orderId;
    }

    @Override
    public void getProduceOrderDetailSuccessed(ProduceProduceOrderBean produceOrderBean) {
        tv_project_name.setText(produceOrderBean.getProjectName());
        tv_engineering_name.setText(" / " + produceOrderBean.getEngineeringName());

        if (null != produceOrderBean.getType()) {
            if ("标准工单".equals(produceOrderBean.getType())) {
                tv_type.setText("A");
            } else if ("返工工单".equals(produceOrderBean.getType())) {
                tv_type.setText("B");
            } else if ("报废工单".equals(produceOrderBean.getType())) {
                tv_type.setText("C");
            } else if ("打样工单".equals(produceOrderBean.getType())) {
                tv_type.setText("D");
            } else if ("免费工单".equals(produceOrderBean.getType())) {
                tv_type.setText("E");
            }
        }

        CommonEnum.ProduceOrderLevel level = produceOrderBean.getLevel();
        if (null != level) {
            if (produceOrderBean.getLevel() == CommonEnum.ProduceOrderLevel.VERY_IMPORTANT) {
                tv_level.setText(" / 一级");
            } else if (produceOrderBean.getLevel() == CommonEnum.ProduceOrderLevel.MORE_IMPORTANT) {
                tv_level.setText(" / 二级");
            } else if (produceOrderBean.getLevel() == CommonEnum.ProduceOrderLevel.GENERALLY_IMPORTANT) {
                tv_level.setText(" / 三级");
            } else if (produceOrderBean.getLevel() == CommonEnum.ProduceOrderLevel.NORMAL) {
                tv_level.setText(" / 四级");
            }
        }

        tv_completed_date.setText(DateUtil.getYyyy_Mm_Dd(produceOrderBean.getCompleteDate()));
        tv_dispath_person.setText(produceOrderBean.getCreateOrderAccountName());
        tv_dispatch_time.setText(DateUtil.getYyyy_Mm_Dd(produceOrderBean.getSendOrderTime()));
        tv_receive_people.setText(produceOrderBean.getReceiveAccountName());
        tv_receive_time.setText(DateUtil.getYyyy_Mm_Dd(produceOrderBean.getReceiveOrderTime()));

        adapter.setData(produceOrderBean.getProductList());
        adapter.selectProductId(selectedProductId);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void getProduceOrderDetailFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public CommonEnum.STATUS getStatus() {
        return status;
    }

    @Override
    public void receiveOrBackOrderSuccessed() {
        if (null != status) {
            if (status == CommonEnum.STATUS.RECEIVED)
            showToast(getResources().getString(R.string.receive_success));
            else if (status == CommonEnum.STATUS.RETURNED)
                showToast(getResources().getString(R.string.return_success));
        }
        Intent mIntent = new Intent();
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        finish();
    }

    @Override
    public void receiveOrBackOrderFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
        isClick = true;
        setButton();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
